home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9005.ZIP / BRIGHT.LST < prev    next >
File List  |  1990-03-23  |  3KB  |  137 lines

  1. _GETTING A HANDLE ON VIRTUAL MEMORY_
  2. by Walter Bright
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. /* Compile with: ZTC wc -ml                                */
  8. /* (Use large model so strcmp() can handle far pointers.)  */
  9.  
  10. #include        <stdio.h>
  11. #include        <stdlib.h>
  12. #include        <string.h>
  13. #include        <ctype.h>
  14. #include        <handle.h>
  15.  
  16. struct tree
  17.     {
  18.         char __handle *word;
  19.         int count;
  20.         struct tree __handle *left;
  21.         struct tree __handle *right;
  22.     };
  23. int readword(char *w, int nbytes)
  24.     {
  25.     int c;
  26.     do  {
  27.         c = getchar();
  28.                 if (c == EOF)
  29.                         return 0;
  30.         }
  31.     while (!isalpha(c));
  32.     do  {
  33.         if (nbytes > 1)
  34.             {
  35.             *w++ = c;
  36.                         nbytes--;
  37.             }
  38.         c = getchar();
  39.         }
  40.     while (isalnum(c));
  41.         *w = 0;
  42.         return 1;
  43.     }
  44. void tree_insert(struct tree __handle * __handle *pt, char *w)
  45.     {
  46.         int cmp;
  47.         struct tree __handle *p;
  48.         while ((p = *pt) != NULL)
  49.         {
  50.         if ((cmp = strcmp(w,p->word)) == 0)
  51.             goto gotit;
  52.                 pt = (cmp < 0) ? &p->left : &p->right;
  53.         }
  54.     p = (struct tree __handle *) handle_calloc(sizeof(struct tree));
  55.         if (!p || (p->word = handle_strdup(w)) == NULL)
  56.         {
  57.         printf("Out of memory\n");
  58.                 exit(EXIT_FAILURE);
  59.         }
  60.     *pt = p;
  61.     gotit:
  62.         p->count++;
  63.     }
  64. tree_print(struct tree __handle *p)
  65.     {
  66.         while (p)
  67.         {
  68.         tree_print(p->left);
  69.         printf("%5d %s\n",p->count,(char far *)p->word);
  70.                 p = p->right;
  71.         }
  72.     }
  73. tree_free(struct tree __handle *p)
  74.     {
  75.     struct tree __handle *pn;
  76.         while (p)
  77.         {
  78.         handle_free(p->word);
  79.                 tree_free(p->left);
  80.                 pn = p->right;
  81.                 handle_free(p);
  82.                 p = pn;
  83.         }
  84.     }
  85. main()
  86.     {
  87.         struct tree __handle *root = NULL;
  88.         char word[32];
  89.         while (readword(word,sizeof(word)))
  90.         tree_insert(&root, word);
  91.     tree_print(root);
  92.     tree_free(root);
  93.         return EXIT_SUCCESS;
  94. }
  95.  
  96.  
  97. [Figure 1: Typical pointer types used in most PC implementations of C]
  98.  
  99.  
  100. void *p;        /* pointer type is default for the memory model */
  101. char far *pc;   /* far (segment and offset pair) pointer        */
  102. int near *p;    /* near (offset only, segment is assumed)       */
  103.  
  104. [Figure 2: Converting pointers from handle to far]
  105.  
  106. R 65,T 5
  107.         int __handle *h;
  108.         struct A __handle *h2;
  109.         int far *f;
  110.         int i;
  111.         extern void func(int far *pi);
  112.  
  113.         f = h;
  114.         *h = i;
  115.         h[3] = *f;
  116.         i = *(h + 6);
  117.         h2->b = i;
  118.         func(h);
  119.         h = (int far *) h;
  120.  
  121.  
  122.  
  123.  
  124. [Figure 3: Example showing that the optimizer is handle aware]
  125.  
  126.     struct { int a,b; } __handle *h;
  127.     h->a = 1;
  128.     h->b = 2;
  129. /* Converted code */
  130.         struct { int a,b; } __handle *h, far *p;
  131.         p = h;
  132.         p->a = 1;
  133.     p->b = 2;
  134.  
  135.  
  136.  
  137.